const { ccclass, property } = cc._decorator;
import n from "./PlayerDataSys"
import l from "./AbTestMgr"
import s from "./PeripheralSystemDataMgr"
import c from "./EventMgr"
import u from "./GameEventType"
import p from "./frameworkManager"
import d from "./EngineUtil"
import h from "./MatchGameData"
import f from "./PageMgr"
import g, { OutSideLevelGuide } from "./PeripheralSystemMgr"
import y from "./UiManage"
import m from "./TopUi"
import UiManager from "./UiManage";
import TopUi from "./TopUi";

const T = -393;
const M = 502;
const P = -7;
const S = 498;
const w = -50;
const A = 502;
const O = -423;
const I = 498;

@ccclass
class TopUiCtrl extends cc.Component {
    public ui: TopUi;

    public onLoad() {
        this.onUILoad();
        this.setTopTagNode();
        this.setGameTopShowNode();
        this.addButtonListen();
        this.addEvent();
        this.checkNotClaimedReward();
        this.updateUserCash();
        this.updateUserGold();
        this.updateClickDayPro();
        this.updateDayAndFundShow();
        // this.dealAbTest();
        this.setTopNodeCanTouch({
            isCanTouch: false
        });
        this.onClockDaysUpdate();
        this.dealOppoReview();
        this.dealReplacePlaceName();
    };
    public start() { };
    public dealOppoReview() {
        var e = s.isHitOppoReviewBan();
        this.ui.fundNode.active = !e;
    };
    public oppoReview() {
        h.isOppoChannel() && (this.ui.cashTipsLabel.getComponent(cc.RichText).string = "到达终点即可提现");
    };
    public dealReplacePlaceName() {
        var e = l.isDmthFlag(), t = h.isOppoChannel(), a = s.isHitOppoReviewBan();
        l.getDmth2Ab1() ? this.ui.cashTipsLabel.getComponent(cc.RichText).string = "到达终点即可提现" : l.getDmth2Ab2() ? this.ui.cashTipsLabel.getComponent(cc.RichText).string = "到达长城即可提现" : e ? this.ui.cashTipsLabel.getComponent(cc.RichText).string = a ? "到达终点即可提现" : "到达京城即可提现" : t && (this.ui.cashTipsLabel.getComponent(cc.RichText).string = "到达终点即可提现");
    };
    public onUILoad() {
        this.ui = this.node.addComponent(m);
    };
    public dealAbTest() {
        return;
        var e = l.isNoGqtcFlag();
        if (l.isOptimizeTravelFundFlag()) {
            this.ui.cashNode.x = -35;
            this.ui.fundNode.x = -395;
            this.ui.redPaperRewardNode.x = w;
            this.ui.redPaperRewardNode.y = A;
            this.ui.fundRewardNode.x = O;
            this.ui.fundRewardNode.y = I;
            this.ui.fundTipNode.scale = 1.1;
        } else {
            this.ui.cashNode.x = -325;
            this.ui.fundNode.x = 20;
            this.ui.redPaperRewardNode.x = T;
            this.ui.redPaperRewardNode.y = M;
            this.ui.fundRewardNode.x = P;
            this.ui.fundRewardNode.y = S;
            this.ui.fundTipNode.scale = 1;
            if (e) {
                this.ui.btn_day_click.x = -460;
                this.ui.fundTipNode.x = 29;
            } else {
                this.ui.btn_day_click.x = 93;
                this.ui.fundTipNode.x = 59;
            }
        }
    };
    public setDayClickShow() {
        var e = l.getIsLcsmzq();
        s.getClockDays() >= 30 && e && (this.ui.btn_day_click.active = false);
    };
    public addEvent() {
        c.listen(u.UPDATE_USER_CASH, this.updateUserCash, this);
        c.listen(u.UPDATE_USER_GOLD, this.updateUserGold, this);
        c.listen(u.DAY_AND_FUD_SHOW, this.updateDayAndFundShow, this);
        c.listen(u.CLICKDAY_PRO, this.updateClickDayPro, this);
        c.listen(u.CASH_AMPLIFY, this.playCashNodeAction, this);
        c.listen(u.FUND_AMPLIFY, this.playFundNodeAction, this);
        c.listen(u.SET_TOP_NODE_CANTOUCH, this.setTopNodeCanTouch, this);
        c.listen(u.UPDATE_COLCK_DAYS, this.onClockDaysUpdate, this);
    };
    public removeEvent() {
        c.ignore(u.UPDATE_USER_CASH, this.updateUserCash, this);
        c.ignore(u.UPDATE_USER_GOLD, this.updateUserGold, this);
        c.ignore(u.DAY_AND_FUD_SHOW, this.updateDayAndFundShow, this);
        c.ignore(u.CLICKDAY_PRO, this.updateClickDayPro, this);
        c.ignore(u.CASH_AMPLIFY, this.playCashNodeAction, this);
        c.ignore(u.FUND_AMPLIFY, this.playFundNodeAction, this);
    };
    public updateUserCash(e?) {
        var t = this;
        void 0 === e && (e = null);
        this.ui.cashCount.getComponent(cc.Label).string = d.getCashBalance(n.cash_balance);
        if (null != e) {
            if (e) {
                var a = e.reward, o = e.isAdd;
                this.ui.redPaperRewardCount.getComponent(cc.Label).string = "+" + a;
                this.ui.redPaperRewardNode.active = true;
                o && cc.tween(this.ui.redPaperRewardNode).to(1.5, {
                    opacity: -100
                }).call(function () {
                    t.ui.redPaperRewardNode.opacity = 255;
                    t.ui.redPaperRewardNode.active = false;
                    t.checkCanWithDraw();
                }).start();
            }
            c.trigger(u.GAME_START_TOP_CASH);
        } else this.checkCanWithDraw();
    };
    public updateUserGold(e?) {
        var t = this;
        void 0 === e && (e = null);
        this.ui.cashCount.getComponent(cc.Label).string = d.getCashBalance(n.gold_balance) + "";
        var a = l.isNoGqtcFlag();
        l.isOptimizeTravelFundFlag() ? this.updateTravelFundExperimentBubbleTips() : a && this.updateFundBubbleTip();
        if (e) {
            var o = e.reward;
            this.ui.fundRewardCount.getComponent(cc.Label).string = "+" + d.getCashBalance(o) + "";
            this.ui.fundRewardNode.active = true;
            cc.tween(this.ui.fundRewardNode).to(1.5, {
                opacity: -100
            }).call(function () {
                t.ui.fundRewardNode.opacity = 255;
                t.ui.fundRewardNode.active = false;
            }).start();
        }
    };
    public setTopNodeCanTouch(e) {
        // if (e) {
        //     this.ui.cashNode.getComponent(cc.Button).interactable = e.isCanTouch;
        //     this.ui.fundNode.getComponent(cc.Button).interactable = e.isCanTouch;
        //     this.ui.btn_set.getComponent(cc.Button).interactable = e.isCanTouch;
        //     this.ui.btn_day_click.getComponent(cc.Button).interactable = e.isCanTouch;
        // }
    };
    public addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_set, this.clickSet, this);
        UiManager.addButtonListen(this.ui.btn_day_click, this.clickDay, this);
        UiManager.addButtonListen(this.ui.cashNode, this.clickWithDraw, this);
        UiManager.addButtonListen(this.ui.cashTipNode, this.clickWithDraw, this);
        UiManager.addButtonListen(this.ui.fundNode, this.clickFundWithDraw, this);
        UiManager.addButtonListen(this.ui.fundNode, this.clickFundWithDraw, this);
      
    };
    public setTopTagNode() {
        var e = s.getTopTagArr();
        if (-1 === e.indexOf(this.ui.redPaper)) {
            var t = {
                type: 0,
                tagNode: this.ui.redPaper
            };
            e.push(t);
        }
        if (-1 === e.indexOf(this.ui.btn_fund)) {
            var a = {
                type: 1,
                tagNode: this.ui.btn_fund
            };
            e.push(a);
        }
        s.setTopTagArr(e);
        3 == e.length && c.trigger(u.SET_EFFECT_TARGETS, {
            nodeArr: e
        });
    };
    public setGameTopShowNode() {
        var e = s.getGameShowTopNodeArr();
        if (-1 === e.indexOf(this.ui.cashNode)) {
            var t = {
                type: 1,
                topNode: this.ui.cashNode
            };
            e.push(t);
        }
        if (-1 === e.indexOf(this.ui.fundNode)) {
            var a = {
                type: 2,
                topNode: this.ui.fundNode
            };
            e.push(a);
        }
        if (-1 === e.indexOf(this.ui.btn_day_click)) {
            var o = {
                type: 4,
                topNode: this.ui.btn_day_click
            }; 
            e.push(o);
        }
        s.setGameShowTopNodeArr(e);
    };
    public checkNotClaimedReward() {
        var e = s.getYesClockData();
        "{}" != JSON.stringify(e) && e && f.showPage("NotclaimedRewardPage");
    };
    public initData() { };
    public updateDayAndFundShow(e?) {
        void 0 === e && (e = null);
        this.ui.btn_day_click.active = true;
        var t = l.isNoGqtcFlag();
        l.isOptimizeTravelFundFlag() ? this.updateTravelFundExperimentBubbleTips() : t ? this.updateFundBubbleTip() : this.ui.fundTipNode.active = false;
        e && (this.ui.btn_day_click.active = e.isShow);
        this.setDayClickShow();
    };
    public updateFundBubbleTip() {
        if (s.getOutsideGuideStep() < OutSideLevelGuide.BREAKTHROUGH) this.ui.fundTipNode.active = false; else {
            var e = s.getCurrentLocation(), t = "";
            if (e >= 11) this.ui.fundTipNode.active = false; else {
                this.ui.fundTipNode.active = true;
                this.ui.tipsBg1.active = false;
                this.ui.tipsBg2.active = true;
                if (e < 5) {
                    var a = 8 - e;
                    t = "<color=#FFFFFF>预计再到达\n</color><color=#FFD200>" + (a > 0 ? a : 0) + "个</color><color=#FFFFFF>城市即可\n</color><color=#FFFFFF>提现</color><color=#FCFF1B>500元</color>";
                    this.ui.fundTipsLabel.getComponent(cc.RichText).string = t;
                } else t = "<color=#FFFFFF>再赚</color><color=#FCFF1B>" + d.getCashBalance(5e4 - n.gold_balance) + "元\n</color><color=#FFFFFF>可提现</color><color=#FCFF1B>500元</color>";
                // this.ui.fundTipNode.getComponent(cc.Animation).play("airbubble1");
                this.ui.fundTipsLabel.getComponent(cc.RichText).string = t;
            }
        }
    };
    public updateTravelFundExperimentBubbleTips() {
        var e = n.gold_balance;
        if (e >= 88800) this.ui.fundTipNode.active = false; else {
            this.ui.fundTipNode.active = true;
            this.ui.tipsBg1.active = false;
            this.ui.tipsBg2.active = true;
            var t = "<color=#FFFFFF>再赚</color><color=#FCFF1B>" + d.getCashBalance(88800 - e) + "元\n</color><color=#FFFFFF>可提现</color><color=#FCFF1B>888元</color>";
            // this.ui.fundTipNode.getComponent(cc.Animation).play("airbubble1");
            this.ui.fundTipsLabel.getComponent(cc.RichText).string = t;
        }
    };
    public playClickSpine(e) {
        if (this.ui.btn_day_click.active) {
            var t = this.ui.spineNode.getComponent(sp.Skeleton);
            this.unscheduleAllCallbacks();
            if (e) d.playSpine(t, "idle_1", true); else {
                var a = ["idle_2", "idle_3"], o = d.getRandomNum(0, 1), i = a[o];
                d.playSpine(t, i, false);
                this.schedule(function () {
                    o = d.getRandomNum(0, 1);
                    i = a[o];
                    d.playSpine(t, i, false);
                }, 3);
            }
        }
    };
    public updateClickDayPro() {
        var e = s.getClockData();
        if (e && "{}" != JSON.stringify(e)) {
            var t = e.current, a = e.total, o = e.get_status;
            if (o != 1) {
                if (!d.isEmpty(t) && !d.isEmpty(a)) {
                    var i = t >= a ? a : t;
                    this.ui.clickDayProNode.getComponent(cc.Label).string = i + "/" + a;
                    this.ui.click_days_tip_bg.active = true;
                    2 == o ? this.playClickSpine(true) : this.playClickSpine(false);
                }
            } else {
                this.ui.click_days_tip_bg.active = false;
                this.playClickSpine(false);
            }
        } else {
            this.playClickSpine(false);
            this.ui.click_days_tip_bg.active = false;
        }
    };
    public playCashNodeAction() {
        var e = this;
        cc.tween(this.ui.cashNode).to(.05, {
            scale: 1.1
        }).to(.05, {
            scale: 1
        }).call(function () {
            e.ui.cashNode.scale = 1;
        }).start();
    };
    public playFundNodeAction() {
        var e = this;
        cc.tween(this.ui.fundNode).to(.05, {
            scale: 1.1
        }).to(.05, {
            scale: 1
        }).call(function () {
            e.ui.fundNode.scale = 1;
        }).start();
    };
    public clickDay() {
        g.getClockTaskList();
    };
    public clickWithDraw() {
        // g.getExtractInfo();
        f.showPage("MatchGameGoldShop", null);
    };
    public clickSet() {
        p.log("设置");
        f.showPage("SettingPage");
    };
    public clickFundWithDraw() {
        g.getTravelExtractInfo();
    };
    public checkCanWithDraw() {
        if (s.isCanWithDraw()) {
            this.ui.btn_payment.active = false;
            this.ui.btn_canPay.active = true;
        } else {
            this.ui.btn_payment.active = true;
            this.ui.btn_canPay.active = false;
        }
    };
    public onClockDaysUpdate() {
        this.ui.btn_day_click.active = true;
        this.ui.cashTipNode.active = false;
        if (s.getClockDays() >= 30 && l.getIsLcsmzq()) {
            this.ui.btn_day_click.active = false;
            this.ui.cashTipNode.active = true;
            this.ui.cashTipNode.getComponent(cc.Animation).play("airbubble2");
        }
    };
    static prefabUrl = "assets/resources/prefabs/TopUi";
    static className = "TopUiCtrl";
    //     return r([v, b("UI/prefabs/TopUiCtrl")], t);
}
